
local module = _G["BlinkHealthTextModule"]

if select(2, UnitClass("player")) ~= "MAGE" or not module then return end

local L_FINGERS_OF_FROST = "Fingers of Frost"
local L_PYROBLAST1 = "Pyroblast"
local L_SHATTER = "Shatter"
local L_FROST_NOVA = "Frost Nova"
local L_DEEP_FREEZE = "DeepFreeze"
local L_ICE_LANCE = "Ice Lance"
local L_ARCANE_CHARGE = "Arcane Charge"
local L_BRAIN_FREEZE = "Brain Freeze"
local L_FROSTJAW = "Frostjaw"
if GetLocale()=="koKR" then
L_FINGERS_OF_FROST = "서리의 손가락"
L_PYROBLAST1 = "불덩이 작렬!"
L_SHATTER = "산산조각"
L_FROST_NOVA = "얼음 회오리"
L_DEEP_FREEZE = "동결"
L_ICE_LANCE = "얼음창"
L_BRAIN_FREEZE = "두뇌 빙결"
L_ARCANE_CHARGE = "비전 충전물"
L_FROSTJAW = "서리투성이 턱"
end

local function getSize(baseSize, expirationTime, duration)
	local size = (expirationTime-GetTime()) / duration
	if size < 0.3 then
		size = baseSize * .8
	elseif size < 0.6 then
		size = baseSize * 1.0
	else
		size = baseSize * 1.2
	end
	return size / 2
end

module.config = {
	[L_FINGERS_OF_FROST] = {
		spellid = 112965
	},
	[L_PYROBLAST1] = {
		spellid = 11366
	},
	[L_BRAIN_FREEZE] = {
		spellid = 44549
	},
	[L_FROSTJAW] = {
		spellid = 102051,
		talent = 9
	},
	[L_SHATTER] = {
		spellid = 12982
	},
	[L_DEEP_FREEZE] = {
		spellid = 44572
	},
}

function module:init()
	self.addon.mainFrame:RegisterEvent("PLAYER_ALIVE")
	self.addon.mainFrame:RegisterEvent("LEARNED_SPELL_IN_TAB")
	self.addon.mainFrame:RegisterEvent("CHARACTER_POINTS_CHANGED")
	self:update()
end

function module:update()
	self.primary = GetSpecialization()
	for auraName, t in pairs(self.config) do
		if t.talent and t.talent > 0 then
			local name, iconTexture, tier, column, selected, available = GetTalentInfo(t.talent);
			if name and available then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					self.addon.mainFrame:RegisterEvent(t.event)
				end
			end
		else
			self.config[auraName].use = false
			if IsPlayerSpell(t.spellid) then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					self.addon.mainFrame:RegisterEvent(t.event)
				end
			end
		end
	end
end

function module:LEARNED_SPELL_IN_TAB(...)
	self:update()
end
module.CHARACTER_POINTS_CHANGED = module.LEARNED_SPELL_IN_TAB
function module:PLAYER_ALIVE(...)
	self.addon.mainFrame:UnregisterEvent("PLAYER_ALIVE")
	self:update()
end

function module:getTargetText()
	local text = ""
	local size = self.addon.db.fontSizeHealth
	local name, rank, icon, count, debufType, duration, expirationTime
	local r, g, b = 1, 1, 1

	if self.config[L_PYROBLAST1].use then
		name, rank, icon, count, debufType, duration, expirationTime = UnitAura("player", L_PYROBLAST1, nil, "HELPFUL")
		if name then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|T%s:%d|t"):format(icon, size)
		end
	end

	if self.config[L_BRAIN_FREEZE].use then
		name, rank, icon, count, debufType, duration, expirationTime = UnitAura("player", L_BRAIN_FREEZE, nil, "HELPFUL")
		if name then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|T%s:%d|t"):format(icon, size)
		end
	end

	if self.config[L_DEEP_FREEZE].use then
		name = nil
		if self.config[L_FINGERS_OF_FROST].use then
			name = UnitAura("player", L_FINGERS_OF_FROST, nil, "HELPFUL")
		end
		if IsUsableSpell(L_DEEP_FREEZE) and (name or UnitAura("target", L_FROST_NOVA, nil, "HARMFUL")) then
			text = text .. (":|T%s:%d|t"):format("Interface\\Icons\\Ability_Mage_DeepFreeze", size / 2)
		end
	end

	return text
end

function module:getTargetHealthColor()
	local name

	if self.config[L_SHATTER].use then
		if self.config[L_FINGERS_OF_FROST].use then
			name = UnitAura("player", L_FINGERS_OF_FROST, nil, "HELPFUL")
			if name then
				return true, 0.75, 0.75, 1.0 -- 파란색? 하늘색?
			end
		end
		name = UnitAura("target", L_FROST_NOVA, nil, "HARMFUL")
		if name then
			return true, 0.75, 0.75, 1.0 -- 파란색? 하늘색?
		end
		if self.config[L_FROSTJAW].use then
			name = UnitAura("target", L_FROSTJAW, nil, "HARMFUL")
			if name then
				return true, 0.75, 0.75, 1.0 -- 파란색? 하늘색?
			end
		end
	end
end

function module:getPlayerText()
	if self.primary ~= 1 then
		return ""
	end
	return self:getAuraCount("player", "비전 충전물", "HARMFUL")
end
